
function Load(promise){
  this.promise = promise || Promise.resolve();
  return this;
}

Load.prototype.load = function(params){
  var params = Array.prototype.slice.call(arguments,0);
    this.promise = this.promise.then(function(){
      var promises = Load.getLoadPromises(params);
      return Promise.all(promises).catch(function(e){console.error(e)});
    });
    return new Load(this.promise);
}

Load.prototype.then = function(callback, isAsync){
  this.promise = this.promise.then(function(data){
      return new Promise(function(resolve, reject){
        if (isAsync) {
          callback(resolve, data);
        } else {
          data = callback(data);
          resolve(data);
        }
      }).catch(function(e){console.error(e)});
    });
    return new Load(this.promise);
}

Load.getLoadPromises = function(params){
    var promises = params.map(function(url){
      return new Promise(function(resolve, reject){
        var type = Load.parseType(url);
        switch (type) {
          case "js":
            Load.loadScript(url, resolve);
            break;
          case "css":
            Load.loadLink(url, resolve);
            break;
          default:
            console.error("unknow type! " + url);
        }
      });
    });
    return promises;
}

Load.parseType = function(url){
  var urlArr = url.split('?');
    var urls = urlArr[0].split('.');
    var end = urls[urls.length-1];
    return end.toLowerCase();
}

Load.loadScript = function(url, resolve){
  if (Load.loaded[url]) {
      resolve();
      return;
    }
  var node = document.createElement('script');
    node.type = 'text/javascript';
    node.charset = 'utf-8';
    // node.async = true;
    node.src = url;
    Load.appendNode(node, url, resolve);
}

Load.loadLink = function(url, resolve){
  if (Load.loaded[url]) {
      resolve();
      return;
    }
  var node = document.createElement('link');
    node.rel = "stylesheet";
    node.href = url;
    document.getElementsByTagName('head')[0].appendChild(node);
    Load.isCSSLoaded(node,url, resolve);
}

Load.isCSSLoaded = function(node, url, resolve){
  var isLoaded = false;
  try {
        if (node.sheet ){
            isLoaded = true;
        }else if (node.styleSheet  ){
            isLoaded = true;
        }else if (node.innerHTML){
            isLoaded = true;
        }
    } catch (ex) {
            //FF下的判断跨域，也不行，路径错了还是抛这个异常,          
        //在发生此异常时，我们默认css加载成功
        if (ex.name && ex.name == 'NS_ERROR_DOM_SECURITY_ERR'){
            isLoaded = true;
        }
    }
    if( isLoaded ){
      resolve();
    }else{
      setTimeout(function(){
        Load.isCSSLoaded(node,url, resolve);
      }, 50);
    }
}

Load.appendNode = function(node, url, resolve){

  if( node.readyState ){
    node.onreadystatechange = function() {
      if ( node.readyState === 'loaded' || node.readyState === 'complete' ) {
        node.onreadystatechange = null;
        Load.loaded[url] = true;
            resolve();
      }
    }
  }else{
    node.onload = function () {
          Load.loaded[url] = true;
          resolve();
      };
      node.onerror = function (e) {
        console.error(JSON.stringify(e));
        resolve();
      };
  }
    
    document.getElementsByTagName('head')[0].appendChild(node);
}

Load.loaded = {};